package com.xrs.xrseducation.school.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xrs.xrseducation.school.entity.School;
import com.xrs.xrseducation.school.entity.SchoolBo;
import com.xrs.xrseducation.school.mapper.SchoolMapper;
import com.xrs.xrseducation.school.service.ISchoolService;
import com.xrs.xrseducation.util.LayuiUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 学科学期班次表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-09-18
 */
@Service
public class SchoolServiceImpl extends ServiceImpl<SchoolMapper, School> implements ISchoolService {

    /**
     * 按条件分页查询所有数据
     * @param page 当前页
     * @param limit 每页显示条数
     * @param schooltype 查询条件一
     * @param schoolname 查询条件二
     * @return
     */
    public LayuiUtils querySchool(Integer page, Integer limit, Integer schooltype, String schoolname){
        LayuiUtils layuiUtils = new LayuiUtils();
        List<SchoolBo> list = new ArrayList<>();
        QueryWrapper qw = new QueryWrapper();
        if(schooltype != null && schooltype != 0){
            qw.eq("schooltype",schooltype);
        }
        if(schoolname != null && !schoolname.equals("")){
            qw.eq("schoolname",schoolname);
        }
        IPage iPage = page(new Page<School>(page,limit),qw);
        for(School s : (List<School>)iPage.getRecords()){
            SchoolBo schoolBo = new SchoolBo();
            BeanUtils.copyProperties(s , schoolBo);
            if(s.getSchooltype() == 1001){
                schoolBo.setSchooltypename("学科");
            }else if(s.getSchooltype() == 1002){
                schoolBo.setSchooltypename("学期");
            }else if(s.getSchooltype() == 1003){
                schoolBo.setSchooltypename("班次");
            }
            list.add(schoolBo);
        }

        int count = (int)iPage.getTotal();

        layuiUtils.setCode(0);
        layuiUtils.setMsg("查询成功");
        layuiUtils.setCount(list.size());
        //layuiUtils.setCount((int)iPage.getTotal());
        layuiUtils.setData(list);
        return layuiUtils;
    }



}
